﻿/*
 * @Autor: PK
 * @Date: 2022/01/14 21:49:SS
 */

using System.Collections.Generic;
using System.Data;
using ShkSimulation.core.dao.input.resource;
using ShkSimulation.core.exceptions;
using ShkSimulation.core.template.agency;
using ShkSimulation.core.util;

namespace ShkSimulation.core.factory {
	
	public delegate Agency CreateSingleAgencyFunction(IReadOnlyDictionary<int, Agency> createdAgencies, CreateParam createParam);

	public class AgencyFactoryByCreateParam: AgencyFactory {
		
		public AgencyFactoryByCreateParam(string resourceKey, CreateSingleAgencyFunction agencyCreateFunction) {
			this.ResourceKey = resourceKey;
			this.AgencyCreateFunction = agencyCreateFunction;
		}

		public string ResourceKey { get; }
		
		private CreateSingleAgencyFunction AgencyCreateFunction { get; set; }

		public Dictionary<int, Agency> Create(Resource resource) {
			DataTable dataTable = resource.GetDataTable();
			Dictionary<int, Agency> result = new Dictionary<int, Agency>();
			List<CreateParam> lines = CollectionUtil.DataTableToCreateParams(dataTable);
			foreach (CreateParam lineData in lines) {
				Agency agency = AgencyCreateFunction(result, lineData);
				if (result.ContainsKey(agency.UniqueId)) {
					throw new ErrorParamException("道路/车站 UniqueId 为空或重复");
				}
				result.Add(agency.UniqueId, agency);
			}
			return result;
		}
		

	}

}